/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Chat;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import TemplateParser.TemplateParsingServlet;

/**
 *
 * @author valiadam
 */
public class SignUpTServlet extends TemplateParsingServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try
        {
            String strOutput = parseTemplateFile("HTML Templates/SignUp/templSignUp.html", request);
            out.print(strOutput);
           
        } finally { 
            out.close();
        }
    }

    /**
     * Reads the username and password from request parameters and creates a new user
     * If creation is successful -> Success message + display the menu
     * else -> Failure message + display the signup form again
     * @param request
     * @return a String containing the generated HTML code
     */


    public String signUp(Object request)
    {
        //get the request parameters...
        HttpServletRequest req = (HttpServletRequest) request;
        String strUser = req.getParameter("username");
        String strPass = req.getParameter("password");
        String strDescr = req.getParameter("description");
        if (strDescr == null)
            strDescr = "";

        String strRet = "";
        boolean bShowSignUpForm = true;

        if (strUser != null && strPass != null)
        {
            //try to create the new user
            boolean bRet = ChatBusinessLogic.Instance().AddNewUser(strUser, strPass, strDescr, req);
            if (bRet)
            {
                //add success message + menu
                strRet += GetSuccessMessage(strUser);
                Menu menu = new Menu();
                strRet += menu.printMenu(req);
                bShowSignUpForm = false;
            }
            else
                strRet += GetFailureMessage();
        }

        //add the sign up form if necessary
        if (bShowSignUpForm)
            strRet += parseTemplateFile("HTML Templates/SignUp/templSignUpForm.html", req);
        
        return strRet;
    }

    private String GetSuccessMessage(String username) {
        return "<font color=\"green\">Sign Up successful, " + username + "</font><br>";
    }

    private String GetFailureMessage() {
        return "<font color=\"red\">Sign Up failed. Try choosing a different username.</font><br>";
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "This is the signup servlet";
    }// </editor-fold>

}
